<script type="text/javascript" src="<?=base_url()?>resource/js/layer/layer.js"></script>
<script type="text/javascript">
    /**
     * 显示添加产品故障面板
     * @constructor
     */
    function ViewAddProductFault(){
        GetData('','');
        $('#faultForm').attr('action',"<?=blang_url('qualityChecks/qualityChecks/AddProductAccessory')?>");
    }
    /**
     * 显示修改产品故障面板
     * @param id
     * @constructor
     */
    function ViewUpdateProductFault(id,typeId,name){
        GetData(typeId,name);
        var index=layer.load(2);
        $.post("<?=blang_url('ajax/ajax_two/GetProductAccessoryInfo')?>",{'id':id},function(data){
            if(data){
                layer.close(index);
                var obj=JSON.parse(data);
                $('#id').val(obj['id']);
                $('#category').val(obj['category']);
                $('#name_ch').val(obj['name_ch']);
                $('#name_en').val(obj['name_en']);
                $('#faultForm').attr('action',"<?=blang_url('qualityChecks/qualityChecks/UpdateProductAccessory')?>");
            }
        });
    }
    /**
     * 提交验证
     * @constructor
     */
    function SubmitVerify(){
        var category=$.trim($('#category').val());
        var id=$('#id').val();
        var name_ch=$.trim($('#name_ch').val());
        var name_en=$.trim($('#name_en').val());
        var in_array='';
        if(category==''){
            layer.msg('产品类型不能为空',{icon:5});
            return false;
        }
        if(name_ch==''){
            layer.msg('附件中文不能为空',{icon:5});
            return false;
        }
        if(name_en==''){
            layer.msg('附件英文不能为空',{icon:5});
            return false;
        }
        $('.dataList').each(function(i,obj){
            var html=$(this).html();
            var idVal=parseInt($(this).attr('title'));
            if(name_ch == html && id != idVal){
                in_array=name_ch;
            }
        });
        if(in_array){
            layer.msg(name_ch+'已经存在',{icon:5});
            return false;
        }
    }
    /**
     * 获取数据
     * @constructor
     */
    function GetData(category,name){
        layer.open({
            type:1,
            title:'产品附件-'+name,
            area:'540px',
            content:$('#productFaultInfo')
        });
        $('#id').val('');
        $('#category').val('');
        $('#name_ch').val('');
        $('#name_en').val('');
        $('#list').html('');
        if(category){
            GetDataHTML(category);
        }
    }
    /**
     * 获取数据HTML
     * @param category
     * @constructor
     */
    function GetDataHTML(category){
        var index=layer.load(2);
        $.post(
            "<?=blang_url('ajax/ajax_two/GetProductAccessory')?>",
            {'typeId':category},
            function(data) {
                if(data){
                    layer.close(index);
                    var dataList=JSON.parse(data);
                    var dataListHtml='';
                    for(var i=0;i<dataList.length;i++){
                        dataListHtml+="<div class='dataList' title='"+dataList[i]['id']+"'>"+dataList[i]['name_ch']+"</div>";
                    }
                    $('#list').html(dataListHtml);
                }
            }
        );
    }
    /**
     * 产品切换
     */
    function selectCategory(){
        var category=$('#category').val();
        GetDataHTML(category);
    }
</script>
<style>
    .dataList{
        background-image: url("http://oms.tomrepair.es/resource/images/m17_u3.png");
        width: 150px;
        height: 30px;
        line-height:30px;
        overflow:hidden;
        text-align: center;
        float: left;
        margin-right: 15px;
        margin-bottom: 10px;
    }
</style>

<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
          <h1>基项管理</h1>
          <?=$this->breadcrumb->generateHTML();?>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-list"></i>产品附件列表</h3>
          </div>
          <div class="panel-body">
            <div class="well" style="padding:0 19px;">
              <div class="row">
                <form action="<?=blang_url('qualityChecks/qualityChecks/ProductAccessoryList')?>">
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="control-label" for="input-name">关键字</label>
                            <input class="form-control" type="text" name="keyword" size="50" placeholder="附件名称">
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="control-label" for="input-name">产品类型</label>
                            <select class="form-control" name="category">
                                <option value="">-全部-</option>
                                <?php foreach($typeData as $row){?>
                                    <option value="<?=$row['id']?>"><?=$row['name']?></option>
                                <?php }?>
                            </select>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group" style="padding-top:37px;">
                            <input class="btn btn-primary" type="submit" value="搜索">
                            <input type="button" class="btn btn-oms" onclick="ViewAddProductFault()" value="添加产品附件">
                        </div>
                    </div>
                </form>
                
              </div>
            </div>
            <div class="table-responsive">
                <table class="table table-bordered table-hover oms-table">
                  <thead>
                    <tr>
                        <th class="text-left">id</th>
                        <th class="text-left">产品类型</th>
                        <th class="text-left">附件中文</th>
                        <th class="text-left">附件英文</th>
                        <th class="text-right">添加时间</th>
                        <th class="text-right">添加人员</th>
                        <th class="text-right">操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php if($productAccessoryData): foreach ($productAccessoryData as $key=>$row):?>
                    <tr>
                        <td class="text-left"><?=$row['id']?></td>
                        <td class="text-left"><?=$row['typeName_ch']?></td>
                        <td class="text-left"><?=$row['name_ch']?></td>
                        <td class="text-left"><?=$row['name_en']?></td>
                        <td class="text-right"><?=date('Y-m-d',$row['add_time'])?></td>
                        <td class="text-right"><?=$row['add_user']?></td>
                        <td class="text-right">
                            <a class="btn btn-primary btn-sm" href="javascript:void(0)" onclick="ViewUpdateProductFault(<?=$row['id']?>,<?=$row['typeId']?>,'<?=$row['typeName_ch']?>')"  title="修改"><i class="fa fa-edit"></i></a>
                            <a class="btn btn-danger btn-sm" href="<?=blang_url('qualityChecks/qualityChecks/DeleteProductAccessory?id='.$row['id'])?>" onclick="return confirm('你确定要删除这条数据吗？')" title="删除"><i class="fa fa-trash-o"></i></a>
                        </td>
                    </tr>

                    <?php endforeach; else:?>
                        <tr><td colspan="7" class="emptyNumber"><?=lang('public_mygdsj')?>....</td></tr>
                    <?php endif; ?>
                </tbody>
                </table>
                <?php if($pageShow):?>
                <div class="page">
                    <?=$pageShow?>
                </div>
                <?php endif;?>
            </div>
          </div>
        </div>
    </div>

    <div style="display: none" id="productFaultInfo">
        <div class="ct_content" style="margin-top: 10px;margin-bottom:10px;margin-left: 20px;" id="infoData">
            <form id="faultForm" method="post" enctype="multipart/form-data">
                <table cellspacing="0" style="border-collapse: collapse;width: 500px;">
                    <tr>
                        <th>产品类型:</th>
                        <td>
                            <input type="hidden" id="id" name="id">
                            <select id="category" name="category" onchange="selectCategory()">
                                <option value="">-请选择-</option>
                                <?php foreach($typeData as $row){?>
                                    <option value="<?=$row['id']?>"><?=$row['name']?></option>
                                <?php }?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>附件中文：</th>
                        <td><input type="text" id="name_ch" name="name_ch" size="45"></td>
                    </tr>
                    <tr>
                        <th>附件英文：</th>
                        <td><input type="text" id="name_en" name="name_en" size="45"></td>
                    </tr>
                    <tr align="center">
                        <td colspan="2"><input type="submit" onclick="return SubmitVerify()" value=" 确 定 "></td>
                    </tr>
                </table>
            </form>
            <br>
            <span style="color:#ff0000">当前附件：</span>
            <br><br>
            <div id="list" style="width: 520px;height: 300px;overflow :auto;"></div>
        </div>
    </div>

</div>
